package leetcode._08_dynic;

import org.junit.Test;

import java.util.*;

/**
 * @author pppppp
 * @date 2022/3/24 9:41
 * 给你一个 只包含正整数 的 非空 数组 nums 。请你判断是否可以将这个数组分割成两个子集，使得两个子集的元素和相等。
 * <p>
 * 示例 1：
 * 输入：nums = [1,5,11,5]
 * 输出：true
 * 解释：数组可以分割成 [1, 5, 5] 和 [11] 。
 * <p>
 * 示例 2：
 * 输入：nums = [1,2,3,5]
 * 输出：false
 * 解释：数组不能分割成两个元素和相等的子集。
 * <p>
 * 提示：
 * 1 <= nums.length <= 200
 * 1 <= nums[i] <= 100
 */
public class _416_分割等和子集 {


    @Test
    public void T_0() {
        int[][] nums = {{14,9,8,4,3,2},{1, 5, 11, 5}, {5, 5, 5, 5}, {1, 2, 3, 5}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8,}, {4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 12, 12, 12, 12, 12, 12, 12, 12, 16, 16, 16, 16, 16, 16, 16, 16, 20, 20, 20, 20, 20, 20, 20, 20, 24, 24, 24, 24, 24, 24, 24, 24, 28, 28, 28, 28, 28, 28, 28, 28, 32, 32, 32, 32, 32, 32, 32, 32, 36, 36, 36, 36, 36, 36, 36, 36, 40, 40, 40, 40, 40, 40, 40, 40, 44, 44, 44, 44, 44, 44, 44, 44, 48, 48, 48, 48, 48, 48, 48, 48, 52, 52, 52, 52, 52, 52, 52, 52, 56, 56, 56, 56, 56, 56, 56, 56, 60, 60, 60, 60, 60, 60, 60, 60, 64, 64, 64, 64, 64, 64, 64, 64, 68, 68, 68, 68, 68, 68, 68, 68, 72, 72, 72, 72, 72, 72, 72, 72, 76, 76, 76, 76, 76, 76, 76, 76, 80, 80, 80, 80, 80, 80, 80, 80, 84, 84, 84, 84, 84, 84, 84, 84, 88, 88, 88, 88, 88, 88, 88, 88, 92, 92, 92, 92, 92, 92, 92, 92, 96, 96, 96, 96, 96, 96, 96, 96, 97, 99}};
        boolean[] ans = {true,true, true, false, true, true};
        for (int i = 0; i < nums.length; i++) {
            // System.out.println(canPartition(nums[i]) == ans[i]);
            // System.out.println(canPartition_1(nums[i]) == ans[i]);
            System.out.println(canPartition_review(nums[i]) == ans[i]);
        }
    }

    /*review——转化为 01 背包的问题来求解*/
    public boolean canPartition_review(int[] nums) {
        int sum = Arrays.stream(nums).sum();
        if (sum % 2 != 0) {
            return false;
        }
        int n = sum / 2 + 1;
        int m = nums.length;
        int[][] dp = new int[m][n];
        for (int i = 0; i < n; i++) {
            if (i >= nums[0]) {
                dp[0][i] = nums[0];
            }
        }

        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (j >= nums[i]) {
                    dp[i][j] = Math.max(dp[i - 1][j],dp[i - 1][j - nums[i]] + nums[i]);
                } else {
                    dp[i][j] = dp[i - 1][j];
                }
                if(dp[i][j] == sum/2){
                    return true;
                }
            }
        }
        return false;
    }

    /*1.使用动态规划 dp[i][j] 是否存在前i个数组成的和恰好等于j */
    public boolean canPartition_1(int[] nums) {
        int sum = Arrays.stream(nums).sum();
        if (sum % 2 == 1) {
            return false;
        }
        Arrays.sort(nums);
        int target = sum >> 1;
        if (nums[nums.length - 1] > target) {
            return false;
        }

        boolean[][] dp = new boolean[nums.length][target + 1];
        dp[0][nums[0]] = true;
        for (int i = 1; i < nums.length; i++) {
            for (int j = 1; j <= target; j++) {
                if (j >= nums[i]) {
                    dp[i][j] = dp[i - 1][j] | dp[i - 1][j - nums[i]];
                } else {
                    dp[i][j] = dp[i - 1][j];
                }

                /*提前结束*/
                if (dp[i][target]) {
                    return true;
                }
            }
        }
        return dp[nums.length - 1][target];
    }

    /*0.回溯超时*/
    public boolean canPartition(int[] nums) {
        int sum = Arrays.stream(nums).sum();
        if (sum % 2 == 1) {
            return false;
        }
        Arrays.sort(nums);
        if (nums[nums.length - 1] > sum >> 1) {
            return false;
        }
        /*查找子集等于sum/2*/
        dfs(nums, sum >> 1, 0, 0);
        return res;
    }


    boolean res = false;

    private void dfs(int[] nums, int target, int curSum, int index) {
        if (target == curSum) {
            res = true;
            return;
        }
        if (index == nums.length || curSum > target) {
            return;
        }

        for (int i = index; i < nums.length && !res; i++) {
            if (i > index && nums[i] == nums[i - 1]) {
                continue;
            }
            curSum += nums[i];
            dfs(nums, target, curSum, i + 1);
            curSum -= nums[i];
        }
    }
}
